package com.woniuxy.email.controller;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {
    // spring提供的发送邮件的工具类
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    @PostMapping("/send/{email}")
    public String sendEmail(@PathVariable("email") String email) {
        // 创建邮件对象
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 发件人是谁
        simpleMailMessage.setFrom(from);
        // 收件人
        simpleMailMessage.setTo(email);
        // 标题、邮件内容
        simpleMailMessage.setSubject("验证码邮件");
        simpleMailMessage.setText(RandomUtil.randomNumbers(4));
        // 发邮件
        javaMailSender.send(simpleMailMessage);
        return "ok";
    }
}
